import json
import pymysql

#连接数据库并插入数据
conn = pymysql.connect(host="localhost", user="root",
                       password="root", db="nba", port=3306, charset='utf8')

with open('player.json') as f:
    contents = json.loads(f.read())
    for content in contents:
        #1.选取球员基本信息中的个人信息
        playerProfile = content['playerProfile']
        playerData = [playerProfile.get('displayName'),playerProfile.get('country'),
                      playerProfile.get('jerseyNo'),playerProfile.get('position'),playerProfile.get('draftYear'),
                      playerProfile.get('experience'),playerProfile.get('height'),playerProfile.get('weight'),
                      playerProfile.get('playerId')]
        #2.选取球员基本信息中的队伍信息

        playerteam =content['teamProfile']
        playerteamData = [playerteam.get('code'),playerteam.get('name')]
        #3.球员信息和球队信息合并为一个列表
        total = playerData + playerteamData
        # print(total)
#
#
        with conn.cursor() as cursor:
            sql = "select displayNameEn from player_info"
            cursor.execute(sql)
            result = cursor.fetchall()
        for res in result:
            for r in res:
                #print(res)
                if r == playerProfile.get('displayNameEn'):
                    print(r)
                    print(playerProfile.get('displayNameEn'))
#                     sql2 =  "INSERT INTO player_info_copy(displayName,country,jerseyNo,position,draftYear,experience,height,weight,playerid,teamCode,teamName) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
#                     effect = cursor.execute(sql, total)
#                     conn.commit()
#                 else:
#                     print("寻找数据失败")
# print('添加数据库成功！')
# # 关闭游标
# cursor.close()
# # 关闭连接
# conn.close()